import selectLoadMore from "@/components/select-load-more.vue";
import { reqProductModelPage, reqBatchClosedLoop, reqOrgList, reqTenantList } from "@/api/api";

const statusType = [{
        key: "1",
        value: "未处理",
    },
    {
        key: "2",
        value: "处理中",
    },
    {
        key: "3",
        value: "已处理",
    },
];
const transstatusType = statusType.reduce((acc, cur) => {
    acc[cur.key] = cur.value;
    return acc;
}, {});

const transdevice = [{
        key: "DoorMagnetism",
        value: "门磁",
    },
    {
        key: "Infrared",
        value: "红外",
    },
    {
        key: "SmokeAlarm",
        value: "烟感",
    },
    {
        key: "GasSensor",
        value: "燃气",
    },
    {
        key: "Camera",
        value: "摄像机",
    },
];
const transdeviceType = transdevice.reduce((acc, cur) => {
    acc[cur.key] = cur.value;
    return acc;
}, {});

const transalarm = [{
        key: "1",
        value: "低电压告警",
    },
    {
        key: "2",
        value: "设备被拆除告警",
    },
    {
        key: "3",
        value: "开门告警",
    },
    {
        key: "4",
        value: "监测到活动告警",
    },
    {
        key: "5",
        value: "烟雾浓度过高告警",
    },
    {
        key: "6",
        value: "故障告警",
    },
    {
        key: "7",
        value: "烟雾传感器故障告警",
    },
    {
        key: "8",
        value: "温度传感器故障告警",
    },
    {
        key: "9",
        value: "拆除故障告警",
    },
    {
        key: "10",
        value: "高温告警",
    },
    {
        key: "11",
        value: "低压故障",
    },
    {
        key: "12",
        value: "高浓度告警",
    },
    {
        key: "13",
        value: "关门告警",
    },
    {
        key: "14",
        value: "断电告警",
    },
    {
        key: "15",
        value: "低电量",
    },
    {
        key: "16",
        value: "通信信号差",
    },
    {
        key: "17",
        value: "消息上报失败",
    },
    {
        key: "18",
        value: "网络初始化失败",
    },
    {
        key: "19",
        value: "压力值超下限",
    },
    {
        key: "20",
        value: "压力值超上限",
    },
    {
        key: "21",
        value: "压力波动超限",
    },
    {
        key: "22",
        value: "压力传感器故障",
    },
    {
        key: "23",
        value: "NB低电量报警",
    },
    {
        key: "24",
        value: "温湿度传感器故障",
    },
    {
        key: "25",
        value: "自检",
    },
    {
        key: "26",
        value: "水浸告警",
    }, {
        key: "27",
        value: "高报警",
    }, {
        key: "28",
        value: "低报警",
    }, {
        key: "30",
        value: "火警告警",
    }, {
        key: "31",
        value: "火情告警",
    }, {
        key: "32",
        value: "手动报警",
    }, {
        key: "33",
        value: "模拟告警",
    }, {
        key: "34",
        value: "漏电告警",
    }, {
        key: "35",
        value: "过压告警",
    }, {
        key: "36",
        value: "欠压告警",
    }, {
        key: "37",
        value: "缺相告警",
    }, {
        key: "38",
        value: "过流告警",
    }, {
        key: "39",
        value: "过载告警",
    }, {
        key: "40",
        value: "电弧告警",
    },
];
const transalarmType = transalarm.reduce((acc, cur) => {
    acc[cur.key] = cur.value;
    return acc;
}, {});

const orderType = [{
        key: "1",
        value: "顺序",
    },
    {
        key: "2",
        value: "逆序",
    },
];
const transOrderType = orderType.reduce((acc, cur) => {
    acc[cur.key] = cur.value;
    return acc;
}, {});

export default {
    data() {
        return {
            // 产品型号
            productId: "",
            classMarkList: [],
            sysInstanceIndex2: 1,
            sysInstanceTotalPages2: 10,
            loading2: false,
            classMarkFlag: false,

            disposeFlag: false,
            beoverFlag: false,
            batchList: [],
            orgName: "",
            selectedValue: "",
            flag2: true
        };
    },
    components: { selectLoadMore },
    mounted() {},
    methods: {
        // 搜索
        async searchClick1() {
            this.page = 1;
            this.limit = 10;
            this.showPopover = false;
            await this.PageMessagePackage();
        },
        // 高级搜索重置
        async resetClick1() {
            this.timeArr = [];
            this.search_val.orgId = "";
            this.search_val.deviceType = "";
            this.search_val.alarmType = "";
            this.search_val.status = "";
            this.search_val.classMark = "";
            this.search_val.name = "";
            this.search_val.tenantId = "";
            this.search_val.order = "";
            // this.search_val.imei = "";
            this.showPopover = false;
            this.classMarkFlag = false;
            this.classMarkList = [];
            this.sysInstanceIndexUnit = 1;
            this.sysInstanceUnitTotalPages = 10;
            this.sysInstanceIndex = 1;
            this.sysInstanceIndex2 = 1;
            this.sysInstanceTotalPages2 = 10;

            this.page = 1;
            this.limit = 10;
            await this.getPreMonth();
            await this.PageMessagePackage();
        },
        // 远程搜索租户
        async remoteMethodEnterprise(query) {
            console.log(query, "query");
            this.tenantName = query;
            this.sysInstanceIndex = 1;
            this.sysInstanceTotalPages = 10;
            this.enterpriseList = [];
            this.getEnterpriseList();
        },
        // 远程搜索单位
        async remoteMethod(query) {
            if (!this.search_val.tenantId)
                return this.$modal.notifyWarning("请先选择租户");
            this.orgName = query;
            this.selectedValue = query; //保留输入的值
            this.sysInstanceIndexUnit = 1;
            this.sysInstanceUnitTotalPages = 10;
            this.projectList = [];
            this.OrgList();
        },
        async loadMoreUnit() {
            if (
                this.sysInstanceIndexUnit >=
                Math.ceil(this.sysInstanceUnitTotalPages / 10)
            )
                return;
            this.sysInstanceIndexUnit++;
            this.loading = true;
            // 获取列表数据的方法
            await this.OrgList();
            this.loading = false;
        },
        // 租户下拉框隐藏
        visibleChangeEnterprise(e) {
            if (!e && this.flag2) {
                // this.tenantName = "";
                // this.enterpriseList = [];
                // this.sysInstanceIndex = 1;
                this.getEnterpriseList();
                this.flag2 = false; //只执行一次
            }
        },
        // 清空租户选框时
        onClearEnterprise() {
            this.sysInstanceIndex = 1;
            this.tenantName = "";
            this.search_val.orgId = "";
            this.enterpriseList = [];
            this.getEnterpriseList();
        },
        // 获取租户列表
        async getEnterpriseList() {
            var res = await reqTenantList(this.sysInstanceIndex, 10);
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.sysInstanceTotalPages = res.data.total;
            this.enterpriseList = [...this.enterpriseList, ...res.data.rows];
        },
        enterpriseGetUnit(e) {
            this.search_val.tenantId = e;
            this.projectList = [];
            this.selectedValue = "";
            this.sysInstanceIndexUnit = 1;
            this.sysInstanceUnitTotalPages = 10;
            if (!e) return;
            this.OrgList();
        },
        // 下拉请求列表
        enterpriseChange(val) {
            if (val && this.flag) {
                this.OrgList();
                this.flag = false; //只执行一次
            }
        },
        // 获取单位列表
        async OrgList(e) {
            var res = await reqOrgList({
                userId: this.user.userId,
                roleName: this.user.roleName,
                tenantId: this.search_val.tenantId,
                orgName: this.orgName,
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.projectList = res.data.rows
        },
        // 产品类型
        async deviceTypeChange(e) {
            this.productList.forEach((item) => {
                if (item.deviceType == e) {
                    this.productId = item.id;
                    return;
                }
            });
            this.classMarkFlag = true;
            this.classMarkList = [];
            this.sysInstanceIndex2 = 1;
            this.sysInstanceTotalPages2 = 10;
            this.search_val.classMark = "";
            this.ProductModelPage();
        },
        // 获取产品型号
        async ProductModelPage() {
            this.classMarkList = [];
            let res = await reqProductModelPage(this.sysInstanceIndex2, 10, {
                productId: this.productId,
            });
            // console.log(res);
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.sysInstanceTotalPages2 = res.data.total;
            this.classMarkList = [...this.classMarkList, ...res.data.rows];
        },
        async loadMore2() {
            if (this.sysInstanceIndex2 >= Math.ceil(this.sysInstanceTotalPages2 / 10))
                return;
            this.sysInstanceIndex2++;
            this.loading2 = true;
            // 获取列表数据的方法
            await this.ProductModelPage();
            this.loading2 = false;
        },
        addClick() {
            this.$refs.refexports.dialogVisible = true;
            this.$refs.refexports.OrgList();
        },
    },
};